var ctx;
var state;
var orderNumber;
var contactor;
var contactorPhone;
var beginTime;
var endTime;


function setCtx(ctx){
	this.ctx = ctx;
}

function ifNull(mess){
	if(mess == null || mess == undefined){
		mess = "";
	}
	return mess;
}

//条件搜索用户列表
function getOrderList(){
	if($("#state").val() != null && $("#state").val() != undefined){
		state = $("#state").val();
	}else{
		state = -1;
	}
	orderNumber = ifNull($("#orderNumber").val());
	//解决ie9以下placeholder与value重复的问题
	if(orderNumber == $("#orderNumber").attr("placeholder")){
		orderNumber = "";
	}
	contactor = ifNull($("#contactor").val());
	//解决ie9以下placeholder与value重复的问题
	if(contactor == $("#contactor").attr("placeholder")){
		contactor = "";
	}
	contactorPhone = ifNull($("#contactorPhone").val());
	//解决ie9以下placeholder与value重复的问题
	if(contactorPhone == $("#contactorPhone").attr("placeholder")){
		contactorPhone = "";
	}
	beginTime = ifNull($("#beginTime").val());
	endTime = ifNull($("#endTime").val()); 
	if(beginTime == $("#beginTime").attr("placeholder")){
		beginTime = "";
	}
	if(endTime == $("#endTime").attr("placeholder")){
		endTime = "";
	}

    var start=new Date(beginTime.replace("-", "/").replace("-", "/"));  
    var end = new Date(endTime.replace("-", "/").replace("-", "/"));  
    
    if(beginTime != "" && endTime == ""){
    	layer.msg("请选择结束时间",{
			icon: 3,
			time: 1000 //2秒关闭（如果不配置，默认是3秒）
		});
    }else if( endTime != "" && beginTime == ""){
    	layer.msg("请选择开始时间",{
			icon: 3,
			time: 1000 //2秒关闭（如果不配置，默认是3秒）
		});
    }else if(end < start){
    	layer.msg("结束时间应大于起始时间",{
			icon: 3,
			time: 1000 //2秒关闭（如果不配置，默认是3秒）
		});
    }else{
        
    	loadContent("/order/orderList?pNo=1&state="+ state + "&orderNumber=" +orderNumber + "&contactor=" + 
    			contactor + "&phone="+contactorPhone+"&beginTime="+beginTime+"&endTime="+endTime);
    } 
}

function reset(){
	orderNumber = $("#orderNumber").val("");
	contactor = $("#contactor").val("");
	contactorPhone = $("#contactorPhone").val("");
	beginTime = $("#beginTime").val("");
	endTime = $("#endTime").val("");
	state = $("#state").val("");
	
}

function changeState(orderId,orderstate,orderType){
	switch(orderstate){
		case '1':
			if(orderType == 1){
				//点击确认，商品需配送，订单不自提，跳转待发货
				orderstate = 2;
			}
			if(orderType == 2){
				//点击确认，商品需配送且订单自提或者商品不需配送，跳转待消费
				orderstate = 9;
			}
			if(orderType == 3){
				//点击拒绝，跳转退款已拒绝
				orderstate = 7;
			}
			break;
			
		case '2':	
			orderstate = 3;
			break;
		case '6':	
			if(orderType == 4){
				//退款申请同意后，状态改为待退款订单（即退款已确认）
				orderstate = 7;
			}
			if(orderType == 5){
				//退款申请拒绝后，状态改为待商家确认
				orderstate = 1;
			}
			break;
			
		case '10':	
			if(orderType == 6){
				//退款货申请同意后，状态改为待退货订单（即退货已确认）
				orderstate = 11;
			}
			if(orderType == 7){
				//退款货申请拒绝后，状态改为已完成订单
				orderstate = 4;
			}
			break;
	}
		
	if(orderId != null && orderstate != null){
	    var proIndex = layer.load(1);
		$.get(ctx + "/order/updateOrderState",{
			id: orderId,
			state: orderstate	
		},function(data){
			data = JSON.parse(data);
			if(data.success){
				layer.msg("操作成功",{
					icon: 1,
					time: 2000 //2秒关闭（如果不配置，默认是3秒）
				},function(){
					loadContent("/order/orderList");
				});
			}else{
				layer.msg(data.msg,{
					icon: 2,
					time: 2000 //2秒关闭（如果不配置，默认是3秒）
				});
			}
			layer.close(proIndex);
		});
	}
}

function orderDetail(orderId){
	loadContent("/order/orderDetail?orderId=" + orderId);
}
